<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: pi_clickandbuy_credit.php 89 2013-09-21 12:04:20Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 89 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 12:04:20 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * @category  PayIntelligent
 * @package   PayIntelligent_ClickandBuy
 * @copyright (C) 2010 PayIntelligent GmbH  <http://www.payintelligent.de/>
 * @license   GPLv2
 */

require_once('includes/application_top.php');
include('../lang/'.$_SESSION['language'].'/admin/pi_clickandbuy.php');

require(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.clickandbuy.php');
$cab = new clickandbuy();	

$shopOrderID = $_GET['oID'];

!empty($_GET['externalID']) ? $externalID = $_GET['externalID'] : $externalID = substr(md5(uniqid(rand())),0,12);
!empty($_GET['intPlaces']) ? $intPlaces = $_GET['intPlaces'] : $intPlaces = '0';	
!empty($_GET['decPlaces']) ? $decPlaces = $_GET['decPlaces'] : $decPlaces = '00';	
!empty($_GET['actionSave']) ? $actionSave = true : $actionSave = false;	

if(!empty($_GET['emailAddress'])) 
	$emailAddress = $_GET['emailAddress'];
	
if(!empty($_GET['itemDesc1'])) 
	$itemDesc1 = $_GET['itemDesc1'];

$authentication = $cab->getCabSettings('clickandbuy');

$postAmount = (float)$intPlaces.'.'.$decPlaces;

$orders = $db->db_query("SELECT customers_email_address FROM ".TABLE_ORDERS." WHERE orders_id ='".$shopOrderID."' ");

!empty($_GET['emailAddress']) ? $emailAddress = $_GET['emailAddress'] : $emailAddress = $orders->fields['customers_email_address'];;

$cabOrderDetails = $db->db_query("SELECT * FROM ".DB_PREFIX."picab_orders WHERE shopOrderID ='".$shopOrderID."' ");

$amount = (float)$cabOrderDetails->fields['amount'] ;
$refunded = (float)$cabOrderDetails->fields['refunded'] + $postAmount;
$cancelled = (float)$cabOrderDetails->fields['cancelled'];
$openAmount = ($amount - $refunded - $cancelled);					
	            
if(($actionSave)) {
	if ($postAmount <= 0.00) {
		$postAmount = 0;
		$messageBox = 'ERROR';
		$errors[] = $errorDescription = 'Invalid Credit amount';
	} 
			
	if(!$errors) {
		$items = array();
		$details = array();
				
		$details['amount'] = $postAmount;
		$details['currency'] = $cabOrderDetails->fields['currency'];
		$details['currency'] = 'EUR';
		$details['emailAddress']= $emailAddress;
		$details['externalID'] = $externalID;
		$items[1]['itemType'] = 'item1Text';	
		$items[1]['textItemDescription'] = $itemDesc1;
		
		$requestResult = $cab->creditRequest($authentication,$details,$items);
		$requestValues = $requestResult['values'];
			
		if ($requestResult['success'] == 1) {
			$messageBox = 'SUCCESS';
			$transactionID = $requestValues['transaction']['transactionID'];
			$transactionStatus = $requestValues['transaction']['transactionStatus'];					

			$db->db_query("INSERT INTO ".DB_PREFIX."picab_transactions (id,shopOrderID,transactionID,externalID,transactionType,description,amount,currency,paid,status,created,modified) VALUES (NULL,'".$shopOrderID."', '".$transactionID."', '".$externalID."', 'credit', '".$itemDesc1."', ".$postAmount.", '".$cabOrderDetails->fields['currency']."', 1, '".$transactionStatus."', NOW(), NOW())");
		} else {
			$messageBox = 'ERROR';
			$msg = $requestValues['detail']['errorDetails']['description'];
		}
	}
}

?>
<table border="0" width="100%" cellspacing="0" cellpadding="2">
	<tr>
		<td>
			<table border="0" width="100%" cellspacing="0" cellpadding="2" height="40">
				<tr>
					<td class="pageHeading"><?php echo CLICKANDBUY_ORDER_CLICKANDBUY.' '.CLICKANDBUY_ORDER_DETAILS_CREDIT; ?></td>
					<td class="pageHeading" align="right">
						<a class="button cab_button" href="pi_clickandbuy_details.php?oID=<?php echo $shopOrderID; ?>">
							<?php echo CLICKANDBUY_ORDER_DETAILS_BACK; ?>
						</a>
					</td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td>
			<?php 
				if ($messageBox == 'SUCCESS') {
					$showMessage = $cab->showMessageSuccess(CLICKANDBUY_ORDER_CREDIT_SUCCESSFUL);
					echo '<div class="clickandbuy_msg">'.$showMessage.'</div>';
				} elseif ($messageBox == 'ERROR') {
					$showMessage = $cab->showMessageError(CLICKANDBUY_ORDER_CREDIT_ERROR.' '.$errorDescription);
					echo '<div class="clickandbuy_msg">'.$showMessage.'</div>';
				}
			?>	        
			<table border="0" width="100%" cellspacing="0" cellpadding="2">
				<tr>
					<td>
						<table cellspacing="0" cellpadding="0" border="0" width="98%">
							<tr>
								<td width="50%">
									<table cellspacing="0" cellpadding="5" border="0" width="400">
										<tbody>	
											<tr>
												<td valign="top" class="infoBoxHeading" colspan="2">
													<b><?php echo CLICKANDBUY_ORDER_DETAILS_CREDIT; ?>: </b>
												</td>                      
											</tr>
											<tr>
												<td class="infoBoxContent" height="20"><?php echo CLICKANDBUY_ORDER_DETAILS_EXTERNALID; ?>:</td>
												<td class="infoBoxContent"><b><?php echo $externalID; ?></b></td>
											</tr>
											<tr>
												<td class="infoBoxContent" height="20" width="120"><?php echo CLICKANDBUY_ORDER_CREDIT_EMAIL; ?>:</td>
												<td class="infoBoxContent">
													<input type="text" value="<?php echo $emailAddress; ?>" name="emailAddress" maxlength="255" size="25" class="editinput emailAddress">
												</td>
											</tr>
											<tr>
												<td class="infoBoxContent" height="20" width="120"><?php echo CLICKANDBUY_ORDER_DETAILS_AMOUNT; ?>:</td>
												<td class="infoBoxContent">
													<input name="intPlaces" type="text" value="<?php echo $intPlaces; ?>" size="3" maxlength="12" class="intPlaces">,
													<input name="decPlaces" type="text" value="<?php echo $decPlaces; ?>" size="2" maxlength="2" class="decPlaces"> <?php echo $cabOrderDetails->fields['currency']; ?>
												</tr>
												<tr>
													<td class="infoBoxContent" height="20" width="120">
														<?php echo CLICKANDBUY_ORDER_DETAILS_DESCRIPTION; ?>:
													</td>
													<td class="infoBoxContent">
														<input type="text" value="<?php echo $itemDesc1; ?>" name="itemDesc1" maxlength="255" size="25" class="itemDesc1">
													</td>
												</tr>
												<tr>
													<td class="infoBoxContent" height="20" width="120">&nbsp;</td>
													<td class="infoBoxContent">
														<a onclick="return getInputs()" class="button credit_href cab_button" href="">
															<?php echo CLICKANDBUY_ORDER_CREDIT_CREDIT_NOW; ?>
														</a>
														<script type="text/javascript">
															function getInputs() {
																var url = '<?php echo 'pi_clickandbuy_credit.php?oID='.$shopOrderID.'&actionSave=true&externalID='.$externalID; ?>';
																url += '&emailAddress='+$('.emailAddress').val();
																url += '&intPlaces='+$('.intPlaces').val();
																url += '&decPlaces='+$('.decPlaces').val();
																url += '&itemDesc1='+$('.itemDesc1').val();
																$('.credit_href').attr('href', url);
															}
														</script>
													</td>
												</tr>
											</tbody>
										</table>
									</td>
								</tr>	
							</table>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>